<?php

namespace Apps\Common\Models;

/**
 * 地区信息和电话区号Model
 * @author     chenxiaolin
 * @date        2016年1月19日
 * @version     2.0.0
 * @copyright   Copyright 2016 meelier.com
 */
class SysAreaZipDistrictPhonecode extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $K_ID;

    /**
     *
     * @var integer
     */
    public $P_ID;

    /**
     *
     * @var string
     */
    public $P_NM;

    /**
     *
     * @var integer
     */
    public $C_ID;

    /**
     *
     * @var string
     */
    public $C_NM;

    /**
     *
     * @var integer
     */
    public $A_ID;

    /**
     *
     * @var string
     */
    public $A_NM;

    /**
     *
     * @var string
     */
    public $FULL_NAME;

    /**
     *
     * @var integer
     */
    public $DISCTRICT_CODE;

    /**
     *
     * @var integer
     */
    public $ZIP_CODE;

    /**
     *
     * @var string
     */
    public $PHONE_CODE;

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'sys_area_zip_district_phonecode';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return SysAreaZipDistrictPhonecode[]
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return SysAreaZipDistrictPhonecode
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * Independent Column Mapping.
     * Keys are the real names in the table and the values their names in the application
     *
     * @return array
     */
    public function columnMap()
    {
        return array(
            'K_ID' => 'K_ID',
            'P_ID' => 'P_ID',
            'P_NM' => 'P_NM',
            'C_ID' => 'C_ID',
            'C_NM' => 'C_NM',
            'A_ID' => 'A_ID',
            'A_NM' => 'A_NM',
            'FULL_NAME' => 'FULL_NAME',
            'DISCTRICT_CODE' => 'DISCTRICT_CODE',
            'ZIP_CODE' => 'ZIP_CODE',
            'PHONE_CODE' => 'PHONE_CODE'
        );
    }

    /**
     * 省份
     * @return array
     */
    public static function getProvince()
    {
        $sql = 'SELECT P_NM value, MIN(K_ID) k FROM sys_area_zip_district_phonecode GROUP BY P_NM ORDER BY k ASC';
        $model = new self;
        return $model->getReadConnection()->query($sql)->fetchAll();
    }

    /**
     * 市
     * @param string $province 省份名称
     * @return array
     */
    public static function getCity($province)
    {
        $sql = 'SELECT C_NM value, MIN(K_ID) k FROM sys_area_zip_district_phonecode WHERE P_NM = :province GROUP BY C_NM ORDER BY k ASC';
        $model = new self;
        return $model->getReadConnection()->query($sql, ['province' => $province])->fetchAll();
    }

    /**
     * 区县
     * @param string $province 省份名称
     * @param string $city 市名称
     * @return array
     */
    public static function getDistrict($province, $city)
    {
        $sql = 'SELECT A_NM value, K_ID id FROM sys_area_zip_district_phonecode WHERE P_NM = :province AND C_NM = :city ORDER BY K_ID ASC';
        $model = new self;
        return $model->getReadConnection()->query($sql, ['province' => $province, 'city' => $city])->fetchAll();
    }

    /**
     * 获取一条城市信息
     * @param int $K_ID 主键id，城市id
     * @return mixed
     */
    public function getOne($K_ID)
    {
        if ($model = self::findFirst('K_ID = ' . $K_ID)) {
            return $model->toArray();
        } else {
            return [];
        }
    }
}
